// pages/my/comments/comments.js
const { getMyComments, updateComment } = require("../../../utils/apis");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    comments: [],
    selectedIndex: NaN,
    show: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    getMyComments()
      .then((res) => {
        this.setData({
          comments: res.data.data,
        });
      })
      .catch((e) => {
        wx.showToast({
          icon: "error",
          title: "获取评论失败",
        });
      });
  },

  onTap(e) {
    this.setData({ selectedIndex: e.target.dataset.index });
    this.showPopup();
  },

  onSubmit(e) {
    const formData = e.detail.value;
    updateComment({
      id: formData.id,
      content: formData.content,
      score: formData.score,
    })
      .then((res) => {
        this.setData({ show: false });
        this.onLoad(null);
      })
      .catch((e) => {
        wx.showToast({
          icon: "error",
          title: "修改评论失败",
        });
      });
  },

  showPopup() {
    this.setData({ show: true });
  },

  onClose() {
    this.setData({ show: false });
  },
});
